package com.group.project.hrms.controller;

import com.alibaba.fastjson.JSON;
import com.group.project.hrms.entity.ContactArchivesWorking;
import com.group.project.hrms.entity.ContactBasic;
import com.group.project.hrms.entity.ResultEntity.ResultEntity;
import com.group.project.hrms.entity.ResultEntity.StatisticsViewEntity;
import com.group.project.hrms.service.IContactArchivesService;
import com.group.project.hrms.service.IContactArchivesWorkingService;
import com.group.project.hrms.service.IContactBasicService;
import com.group.project.hrms.utils.AttendanceUtil;
import com.group.project.hrms.utils.DateUtil;
import com.group.project.hrms.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class AnalysisController {

    @Autowired
    private IContactArchivesWorkingService iContactArchivesWorkingService;

    @Autowired
    private DateUtil dateUtil;

    @Autowired
    private LogUtil logUtil;


    /**
     * 计算并返回HRMS前端统计分析页面需要的数据（homepage）
     * @param map 包含年份信息的Map //格式应为：2022
     * @param request http请求
     * @return 标准返回值对象
     */
    @RequestMapping("/basic/getEmployeeStatisticsViewData")
    @ResponseBody
    public ResultEntity getEmployeeStatisticsViewData(@RequestBody Map map, HttpServletRequest request){
        ResultEntity resultEntity = new ResultEntity();
        Object staffName = request.getAttribute("staffName");
        if(null != map){
            Object year = map.get("year");
            if(null != year){
                int currentYear = Integer.parseInt(year.toString());
                //System.out.println("当前需要查询的年份为：" + currentYear);
                //List<ContactBasic> contactBasicList = iContactBasicService.selectAllContactBasic();
                List<ContactArchivesWorking> archivesWorkings = iContactArchivesWorkingService.selectAllContactArchivesWorking();
                List<StatisticsViewEntity> viewEntities = new ArrayList<>();
                int[] joinCounts = {0,0,0,0,0,0,0,0,0,0,0,0}; //存储该年份每个月的入职人数
                int[] leaveCounts = {0,0,0,0,0,0,0,0,0,0,0,0}; //存储该年份每个月离职的人数
                int[] monthTotal = {0,0,0,0,0,0,0,0,0,0,0,0}; //存储了每个月的员工的总数
                if(null != archivesWorkings && archivesWorkings.size() != 0){
                    for (ContactArchivesWorking working : archivesWorkings
                         ) {
                        Date joinDate = working.getOnboardActualDate();
                        Date leaveDate = working.getLeaveActualDate();

                        int joinMonth = (null != joinDate ? dateUtil.getMonthByDate(joinDate) : 0);
                        int leaveMonth = (null != leaveDate ? dateUtil.getMonthByDate(leaveDate) : 0);

                        int joinYear = (null != joinDate ? dateUtil.getYearByDate(joinDate) : 0);
                        int leaveYear = (null != leaveDate ? dateUtil.getYearByDate(leaveDate) : 0);

                        //计算传入年份的每月总人数
                        if(null != joinDate){
                            Date lastJan = dateUtil.getLastDateByString(currentYear + "-" + "01-01 00:00:00");
                            if(joinDate.getTime() <= lastJan.getTime() && (null == leaveDate || leaveDate.getTime() > lastJan.getTime())){
                                monthTotal[0] = monthTotal[0] + 1;
                            }
                            Date lastFeb = dateUtil.getLastDateByString(currentYear + "-" + "02-01 00:00:00");
                            if(joinDate.getTime() <= lastFeb.getTime() && (null == leaveDate || leaveDate.getTime() > lastFeb.getTime())){
                                monthTotal[1] = monthTotal[1] + 1;
                            }
                            Date lastMar = dateUtil.getLastDateByString(currentYear + "-" + "03-01 00:00:00");
                            if(joinDate.getTime() <= lastMar.getTime() && (null == leaveDate || leaveDate.getTime() > lastMar.getTime())){
                                monthTotal[2] = monthTotal[2] + 1;
                            }
                            Date lastApr = dateUtil.getLastDateByString(currentYear + "-" + "04-01 00:00:00");
                            if(joinDate.getTime() <= lastApr.getTime() && (null == leaveDate || leaveDate.getTime() > lastApr.getTime())){
                                monthTotal[3] = monthTotal[3] + 1;
                            }
                            Date lastMay = dateUtil.getLastDateByString(currentYear + "-" + "05-01 00:00:00");
                            if(joinDate.getTime() <= lastMay.getTime() && (null == leaveDate || leaveDate.getTime() > lastMay.getTime())){
                                monthTotal[4] = monthTotal[4] + 1;
                            }
                            Date lastJun = dateUtil.getLastDateByString(currentYear + "-" + "06-01 00:00:00");
                            if(joinDate.getTime() <= lastJun.getTime() && (null == leaveDate || leaveDate.getTime() > lastJun.getTime())){
                                monthTotal[5] = monthTotal[5] + 1;
                            }
                            Date lastJul = dateUtil.getLastDateByString(currentYear + "-" + "07-01 00:00:00");
                            if(joinDate.getTime() <= lastJul.getTime() && (null == leaveDate || leaveDate.getTime() > lastJul.getTime())){
                                monthTotal[6] = monthTotal[6] + 1;
                            }
                            Date lastAug = dateUtil.getLastDateByString(currentYear + "-" + "08-01 00:00:00");
                            if(joinDate.getTime() <= lastAug.getTime() && (null == leaveDate || leaveDate.getTime() > lastAug.getTime())){
                                monthTotal[7] = monthTotal[7] + 1;
                            }
                            Date lastSep = dateUtil.getLastDateByString(currentYear + "-" + "09-01 00:00:00");
                            if(joinDate.getTime() <= lastSep.getTime() && (null == leaveDate || leaveDate.getTime() > lastSep.getTime())){
                                monthTotal[8] = monthTotal[8] + 1;
                            }
                            Date lastOct = dateUtil.getLastDateByString(currentYear + "-" + "10-01 00:00:00");
                            if(joinDate.getTime() <= lastOct.getTime() && (null == leaveDate || leaveDate.getTime() > lastOct.getTime())){
                                monthTotal[9] = monthTotal[9] + 1;
                            }
                            Date lastNov = dateUtil.getLastDateByString(currentYear + "-" + "11-01 00:00:00");
                            if(joinDate.getTime() <= lastNov.getTime() && (null == leaveDate || leaveDate.getTime() > lastNov.getTime())){
                                monthTotal[10] = monthTotal[10] + 1;
                            }
                            Date lastDec = dateUtil.getLastDateByString(currentYear + "-" + "12-01 00:00:00");
                            if(joinDate.getTime() <= lastDec.getTime() && (null == leaveDate || leaveDate.getTime() > lastDec.getTime())){
                                monthTotal[11] = monthTotal[11] + 1;
                            }
                        }

                        //计算传入年份的月度离职人数和入职人数
                        for (int i = 0; i < 2; i++) {
                            int month;
                            if(i == 0){
                                month = joinMonth;
                            }else {
                                month = leaveMonth;
                            }
                            switch (month){
                                case 1:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[0] = joinCounts[0] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[0] = leaveCounts[0] + 1;
                                    }
                                    break;
                                case 2:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[1] = joinCounts[1] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[1] = leaveCounts[1] + 1;
                                    }
                                    break;
                                case 3:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[2] = joinCounts[2] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[2] = leaveCounts[2] + 1;
                                    }
                                    break;
                                case 4:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[3] = joinCounts[3] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[3] = leaveCounts[3] + 1;
                                    }
                                    break;
                                case 5:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[4] = joinCounts[4] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[4] = leaveCounts[4] + 1;
                                    }
                                    break;
                                case 6:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[5] = joinCounts[5] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[5] = leaveCounts[5] + 1;
                                    }
                                    break;
                                case 7:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[6] = joinCounts[6] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[6] = leaveCounts[6] + 1;
                                    }
                                    break;
                                case 8:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[7] = joinCounts[7] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[7] = leaveCounts[7] + 1;
                                    }
                                    break;
                                case 9:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[8] = joinCounts[8] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[8] = leaveCounts[8] + 1;
                                    }
                                    break;
                                case 10:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[9] = joinCounts[9] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[9] = leaveCounts[9] + 1;
                                    }
                                    break;
                                case 11:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[10] = joinCounts[10] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[10] = leaveCounts[10] + 1;
                                    }
                                    break;
                                case 12:
                                    if(i == 0 && joinYear == currentYear){
                                        joinCounts[11] = joinCounts[11] + 1;
                                    }else if(i == 1 && leaveYear == currentYear){
                                        leaveCounts[11] = leaveCounts[11] + 1;
                                    }
                                    break;
                            }
                        }
                    }
                }
                //System.out.println(JSON.toJSONString(joinCounts));
                //System.out.println(JSON.toJSONString(leaveCounts));
                //System.out.println(JSON.toJSONString(monthTotal));
                //汇总并计算月度数据
                for (int i = 0; i < 3; i++) {
                    int[] tempArray;
                    if(i == 0){
                        tempArray = joinCounts;
                    }else if(i == 1){
                        tempArray = leaveCounts;
                    }else{
                        tempArray = monthTotal;
                    }
                    for (int j = 0; j < 12; j++) {
                        //计算一月份数据
                        if(j == 0 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-01");
                            targetView.setCount(tempArray[0]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 0 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-01");
                            targetView.setCount(tempArray[0]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 0 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-01");
                            targetView.setCount(tempArray[0]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算二月份数据
                        if(j == 1 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-02");
                            targetView.setCount(tempArray[1]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 1 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-02");
                            targetView.setCount(tempArray[1]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 1 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-02");
                            targetView.setCount(tempArray[1]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算三月份数据
                        if(j == 2 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-03");
                            targetView.setCount(tempArray[2]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 2 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-03");
                            targetView.setCount(tempArray[2]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 2 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-03");
                            targetView.setCount(tempArray[2]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算四月份数据
                        if(j == 3 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-04");
                            targetView.setCount(tempArray[3]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 3 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-04");
                            targetView.setCount(tempArray[3]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 3 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-04");
                            targetView.setCount(tempArray[3]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算五月份数据
                        if(j == 4 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-05");
                            targetView.setCount(tempArray[4]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 4 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-05");
                            targetView.setCount(tempArray[4]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 4 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-05");
                            targetView.setCount(tempArray[4]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算六月份数据
                        if(j == 5 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-06");
                            targetView.setCount(tempArray[5]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 5 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-06");
                            targetView.setCount(tempArray[5]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 5 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-06");
                            targetView.setCount(tempArray[5]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算七月份数据
                        if(j == 6 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-07");
                            targetView.setCount(tempArray[6]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 6 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-07");
                            targetView.setCount(tempArray[6]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 6 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-07");
                            targetView.setCount(tempArray[6]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算八月份数据
                        if(j == 7 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-08");
                            targetView.setCount(tempArray[7]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 7 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-08");
                            targetView.setCount(tempArray[7]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 7 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-08");
                            targetView.setCount(tempArray[7]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算九月份数据
                        if(j == 8 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-09");
                            targetView.setCount(tempArray[8]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 8 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-09");
                            targetView.setCount(tempArray[8]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 8 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-09");
                            targetView.setCount(tempArray[8]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算十月份数据
                        if(j == 9 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-10");
                            targetView.setCount(tempArray[9]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 9 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-10");
                            targetView.setCount(tempArray[9]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 9 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-10");
                            targetView.setCount(tempArray[9]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算十一月份数据
                        if(j == 10 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-11");
                            targetView.setCount(tempArray[10]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 10 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-11");
                            targetView.setCount(tempArray[10]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 10 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-11");
                            targetView.setCount(tempArray[10]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                        //计算十二月份数据
                        if(j == 11 && i == 0){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-12");
                            targetView.setCount(tempArray[11]);
                            targetView.setName("入职人数");
                            viewEntities.add(targetView);
                        }else if(j == 11 && i == 1){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-12");
                            targetView.setCount(tempArray[11]);
                            targetView.setName("离职人数");
                            viewEntities.add(targetView);
                        }else if(j == 11 && i == 2){
                            StatisticsViewEntity targetView = new StatisticsViewEntity();
                            targetView.setTime(currentYear + "-12");
                            targetView.setCount(tempArray[11]);
                            targetView.setName("员工总数");
                            viewEntities.add(targetView);
                        }
                    }
                }
                //logUtil.insertSystemEventLog(staffName.toString(), new Date(), "访问了系统总览中的数据分析功能", "考勤");
                resultEntity.setCode("S");
                resultEntity.setMsg("Success");
                resultEntity.setDataList(viewEntities);
                return resultEntity;
            }
        }else {
            resultEntity.setCode("F");
            resultEntity.setMsg("传入参数不正确");
            return resultEntity;
        }
        resultEntity.setCode("S");
        resultEntity.setMsg("Success");
        return resultEntity;
    }


}
